<template>
	<gracePage :customHeader="false">
		<view slot="gBody" class="grace-body">
			<!-- 购物车为空 -->
			<view v-if="items.length < 1" style="margin-top:150px;">
				<graceEmpty>
					<view slot="img" class="empty-view">
						<!-- 请根据您的项目要求制作并更换为空图片 -->
						<image class="empty-img" mode="widthFix" src="https://staticimgs.oss-cn-beijing.aliyuncs.com/empty.png"></image>
					</view>
					<text slot="text" class="grace-text-small grace-gray">您的购物车空空如也~</text>
				</graceEmpty>
			</view>
			<view style="height:38rpx;"></view>
			<!-- 购物车主结构  -->
				<view class="grace-shoppingcard-goods" v-for="(goods, indexItem) in items" :key="indexItem">
					<view class="grace-shoppingcard-goods-checkbtn">
						<graceCheckBtn :checked="goods.checked" @change="itemChange" 
						:parameter="[indexItem]"></graceCheckBtn>
					</view>
					<image :src="goods.img" class="grace-shoppingcard-goods-image" mode="widthFix"></image>
					<view class="grace-shoppingcard-goods-body">
						<view class="grace-shoppingcard-goods-title">{{goods.goodsName}}</view>
						<text class="grace-img-card-price">￥{{goods.price}}</text>
						<text class="grace-line-through grace-text-small grace-gray">￥{{goods.price}}</text>
					</view>
				</view>
		</view>
		<view slot="gFooter" class="grace-nowrap grace-flex-vcenter grace-border-t" style="background-color:#FFFFFF;">
			<view class="grace-shoppingcard-checkbtn grace-nowrap grace-flex-vcenter">
				<graceCheckBtn :checked="true" @change="itemChangeAll">
					<text class="grace-text">{{selectText}}</text>
				</graceCheckBtn>
			</view>
			<view class="grace-shoppingcard-count grace-nowrap grace-flex-vcenter">
				<text class="grace-text">合计 :</text>
				<text style="font-size:32rpx; color:#F00;">￥{{totalprice}}</text>
			</view>
			<view class="grace-shoppingcard-checkout grace-bg-red" @tap="checkout">立即结算</view>
		</view>
	</gracePage>
</template>
<script>
import gracePage from "../../graceUI/components/gracePage.vue";
import graceEmpty from "../../graceUI/components/graceEmptyNew.vue";
import graceCheckBtn from '../../graceUI/components/graceCheckBtn.vue';
import graceNumberBox from '../../graceUI/components/graceNumberBox.vue';
export default {
	data() {
		return {
			// 总价
			totalprice : '',
			// 选择文本
			selectText : '全选',
			// 购物车数据 可以来自 api 请求或本地数据
		    items: [
		    	{
		    		"goodsId": 1,
		    		"goodsName": "hcoder 演示商品",
		    		"price": 10.00,
		    		"count": 1,
		    		"img": "https://m.360buyimg.com/babel/jfs/t1/3730/7/3438/394579/5b996f2eE1727c59e/373cf10d42a53b72.jpg",
		    		"checked" : true
		    	},
		    	{
		    		"goodsId": 2,
		    		"goodsName": "dcloud 演示商品",
		    		"price": 20.00,
		    		"count": 1,
		    		"img": "https://img14.360buyimg.com/n7/jfs/t1/1156/8/14017/123589/5bd9a4e8E7dbd4a15/70fbbccdf8811111.jpg",
		    		"checked" : true
		    	}
		    ]	
		}
	},
	onLoad : function(){
		// 初始化时候计算总价，如果使用api 获取购物车项目在 api 请求完成后执行此函数
		// this.listShoppingcard();
		this.countTotoal();
	},
	methods:{
		//退回
		goBack : function(){
		    uni.navigateBack({});
		},
		//计算总计函数
		countTotoal:function(){
			var total = 0;
			// for (var i = 0; i < this.shoppingCard.length; i++){
			// 	for (var ii = 0; ii < this.shoppingCard[i].items.length; ii++){
			// 		if(this.shoppingCard[i].items[ii].checked){
			// 			total += Number(this.shoppingCard[i].items[ii].price) * Number(this.shoppingCard[i].items[ii].count);
			// 		}
			// 	}
			// }
			this.totalprice = total;
		},
		numberChange:function(data){
			// this.shoppingCard[data[2]].items[data[1]].count = data[0];
			//计算总价
			this.countTotoal();
		},
		removeGoods : function(e){
			var index = e.currentTarget.id.replace('removeIndex_', '');
			index = index.split('_');
			var index1 = Number(index[0]);
			var index2 = Number(index[1]);
			uni.showModal({
				title: '确认提醒',
				content: '您确定要移除此商品吗？',
				success:(e)=>{
					if (e.confirm) {
						// this.shoppingCard[index1].items.splice(index2, 1);
						// 是否全部删除包含商铺
						// if(this.shoppingCard[index1].items.length < 1){
						// 	this.shoppingCard.splice(index1, 1);
						// }
						//计算总价
						this.countTotoal();
					}
				}
			})
		},
		//结算
		async checkout(){
			// let data = await this.$apis.listShoppingcard();
		},
		// 店铺选中按钮状态切换
		shopChange : function (e) {
			var index = Number(e[1]);
			// this.shoppingCard[index].checked = e[0];
			// for(let i = 0; i < this.shoppingCard[index].items.length; i++){
			// 	this.shoppingCard[index].items[i].checked = e[0];
			// }
			// this.shoppingCard.splice(index, this.shoppingCard[index]);
			this.countTotoal();
		},
		// 商品选中
		itemChange : function (e) {
			var indexs = e[1].toString();
			var index = indexs.split('-');
			index[0] = Number(index[0]);
			index[1] = Number(index[1]);
			// this.shoppingCard[index[0]].items[index[1]].checked = e[0];
			// this.shoppingCard.splice(index[0], this.shoppingCard[index[0]]);
			var checkedNum = 0;
			// for(let i = 0; i < this.shoppingCard[index[0]].items.length; i++){
			// 	if(!this.shoppingCard[index[0]].items[i].checked){checkedNum++;}
			// }
			// if(checkedNum < 1){
			// 	this.shoppingCard[index[0]].checked = true;
			// }else{
			// 	this.shoppingCard[index[0]].checked = false;
			// }
			// this.shoppingCard = this.shoppingCard;
			this.countTotoal();
		},
		itemChangeAll : function (e) {
			this.selectText = e[0] ? '全选' : '全不选';
			// for(var i = 0; i < this.shoppingCard.length; i++){
			// 	this.shoppingCard[i].checked = e[0];
			// 	for(var ii = 0; ii < this.shoppingCard[i].items.length; ii++){
			// 		this.shoppingCard[i].items[ii].checked = e[0];
			// 	}
			// }
			this.countTotoal();
		},
		//获取详情
		async listShoppingcard() {
			let data = await this.$apis.listShoppingcard();
			this.items = data;
		},
	},
	components:{
		gracePage,
		graceNumberBox,
		graceEmpty,
		graceCheckBtn
	}
}
</script>
<style>
page{background:#FFFFFF;}
.empty-view{width:280rpx; height:280rpx; border-radius:280rpx; background-color:#F6F7F8; margin-top:30rpx;}
.empty-img{width:220rpx; height:200rpx; margin:40rpx; border-radius:200rpx;}
.grace-shoppingcard{background:#FFFFFF; border-radius:8rpx; padding:20rpx; margin-bottom:20rpx;}
.grace-shopp-name{width:500rpx;}
.grace-shopp-go{width:120rpx;}
.grace-shoppingcard-goods{margin:10rpx 0; display:flex; flex-wrap:nowrap;}
.grace-shoppingcard-goods-checkbtn{width:60rpx; flex-shrink:0;}
.grace-shoppingcard-goods-image{width:120rpx; margin-right:20rpx; flex-shrink:0;}
.grace-shoppingcard-goods-body{width:100%;}
.grace-shoppingcard-goods-title{line-height:1.4em;}
.grace-shoppingcard-goods-price{color:#F00; font-size:32rpx; display:flex; flex-wrap:nowrap; justify-content:space-between;}
.grace-shoppingcard-goods-number{padding:2px 0;}
.grace-shoppingcard-remove{display:block; text-align:right; line-height:50rpx; margin-top:30rpx; color:#CCCCCC; font-size:24rpx;}
.grace-shoppingcard-checkbtn{width:200rpx; margin-left:50rpx; flex-shrink:0;}
.grace-shoppingcard-count{width:600rpx;}
.grace-shoppingcard-checkout{width:280rpx; height:100rpx; line-height:100rpx; font-size:28rpx; text-align:center; flex-shrink:0;}
</style>